function preLoad() {
	if (!this.support.loading) {
		alert("You need the Flash Player 9.028 or above to use SWFUpload.");
		return false;
	}
}
function loadFailed() {
	alert("Something went wrong while loading SWFUpload. If this were a real application we'd clean up and then give you an alternative");
}

function fileQueueError(file, errorCode, message) {
	try {
		var errorName = "";
		switch (errorCode) {
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				errorName = (message>0) ? "错误：仅可以选择 "+message+" 个文件！" : "已经上传了文件，请先删除再上传！";
				break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				errorName = "错误：文件大小为0！";
				break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				errorName = "错误：文件大小超过要求！";
				break;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				errorName = "错误：文件类型不符合要求！";
				break;
			default:
				errorName = errorCode+": "+message;
				break;
		}
		if (errorName !== "")
		{
			alert(errorName);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			this.startUpload(this.getFile(0).ID);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadProgress(file, bytesLoaded) {

	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);

		var progress = new FileProgress(file,  this.customSettings.upload_target);
		progress.setProgress(percent);
		progress.setStatus("上传中...");
		progress.toggleCancel(true, this);
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file,  this.customSettings.upload_target);
		var serverDataJson = jQuery.parseJSON(serverData);
		if (serverDataJson.status > 0) {
			if (this.customSettings.upload_filenum==1)
			{
				addFlashImageOne(serverDataJson, this.customSettings.upload_sign);
			}
			else
			{
				addFlashImage(serverDataJson, this.customSettings.upload_sign);
			}
			progress.setStatus("上传完成。");
			progress.toggleCancel(false);
		} else {
			progress.setStatus("上传错误。");
			progress.toggleCancel(false);
			alert("错误："+serverDataJson.errorinfo);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	try {
		if (this.getStats().files_queued > 0) {
			this.startUpload(this.getFile(0).ID);
		} else {
			var progress = new FileProgress(file,  this.customSettings.upload_target);
			progress.setComplete();
			progress.setStatus("文件上传完毕。");
			progress.toggleCancel(false);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	var progress;
	try {
		switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				try {
					progress = new FileProgress(file,  this.customSettings.upload_target);
					progress.setCancelled();
					progress.setStatus("已取消");
					progress.toggleCancel(false);
				}
				catch (ex1) {
					this.debug(ex1);
				}
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				try {
					progress = new FileProgress(file,  this.customSettings.upload_target);
					progress.setCancelled();
					progress.setStatus("已停止");
					progress.toggleCancel(true);
				}
				catch (ex2) {
					this.debug(ex2);
				}
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				try {
					progress = new FileProgress(file,  this.customSettings.upload_target);
					progress.setCancelled();
					progress.setStatus("已达到最大数量限制");
					progress.toggleCancel(true);
				}
				catch (ex3) {
					this.debug(ex3);
				}
				break;
			default:
				alert(errorCode+": "+message);
				break;
		}
	} catch (ex3) {
		this.debug(ex3);
	}
}

function addFlashImageOne(fileinfo, strsign)
{
	var tempsign = strsign;
	if ($("#albumup_thumb_id_"+tempsign))
	{
		$("#albumup_thumb_id_"+tempsign).remove();
	}
	var temphtml = '<span class="albumup_thumb" id="albumup_thumb_id_'+tempsign+'"><a href="javascript:void(0);" onclick="delFlashImage(this, \''+tempsign+'\');"><img src="'+WEBURLPATH+'/images/delpic.gif" alt="" /></a><img src="'+WEBURLPATH+'/showimage.php?w=200&h=100&f='+fileinfo.abspathfile+'" alt="" /><input type="hidden" name="'+tempsign+'_input_name" value="'+fileinfo.pathfile+'" /></span>';
	$("#albumup_thumb_"+tempsign).append(temphtml);
	var stats = albumobj[tempsign].getStats();
	if (stats.successful_uploads > 0)
	{
		stats.successful_uploads--;
		albumobj[tempsign].setStats(stats);
	}
}

function addFlashImage(fileinfo, strsign)
{
	var tempsign = strsign;
	var tempid = ++albumobj[tempsign+'_idcount'];
	var temphtml = '<span class="albumup_thumb"><a href="javascript:void(0);" onclick="delFlashImage(this, \''+tempsign+'\');"><img src="'+WEBURLPATH+'/images/delpic.gif" alt="" /></a><img src="'+WEBURLPATH+'/showimage.php?w=140&h=140&f='+fileinfo.abspathfile+'" alt="" title="" /><br /><input type="hidden" name="'+tempsign+'_input_name['+tempid+']" value="'+fileinfo.pathfile+'" /><input type="hidden" name="'+tempsign+'_input_rate['+tempid+']" value="'+fileinfo.imagerate+'" />描述：<input type="text" name="'+tempsign+'_input_info['+tempid+']" value="" size="12" /><br />排序：<input type="text" name="'+tempsign+'_input_order['+tempid+']" value="0" size="3" />&nbsp;封面：<input type="radio" name="'+tempsign+'_input_cover" value="'+tempid+'" /></span>';
	$("#albumup_thumb_"+tempsign).append(temphtml);
}

function delFlashImage(obj, strsign)
{
	var tempsign = strsign;
	$(obj).parent().remove();
	$("#albumup_progress_"+tempsign).html('');
	/*
	var stats = albumobj[tempsign].getStats();
	if (stats.successful_uploads > 0)
	{
		stats.successful_uploads--;
		albumobj[tempsign].setStats(stats);
	}
	if ($("#albumup_progress_"+tempsign).css('display')=='none')
	{
		$("#albumup_button_"+tempsign).css('display', 'inline-block');
		$("#albumup_progress_"+tempsign).html('');
		$("#albumup_progress_"+tempsign).css('display', 'inline-block');
	}
	*/
}

function fadeIn(element, opacity) {
	var reduceOpacityBy = 5;
	var rate = 30;	// 15 fps


	if (opacity < 100) {
		opacity += reduceOpacityBy;
		if (opacity > 100) {
			opacity = 100;
		}

		if (element.filters) {
			try {
				element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
			}
		} else {
			element.style.opacity = opacity / 100;
		}
	}

	if (opacity < 100) {
		setTimeout(function () {
			fadeIn(element, opacity);
		}, rate);
	}
}



/* ******************************************
 *	FileProgress Object
 *	Control object for displaying file info
 * ****************************************** */

function FileProgress(file, targetID) {
	this.fileProgressID = "divFileProgress_"+targetID;

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("span");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var progressStatus = document.createElement("span");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		fadeIn(this.fileProgressWrapper, 0);

	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.fileProgressElement.childNodes[1].firstChild.nodeValue = file.name;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.childNodes[1].innerHTML = '&nbsp;';
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfuploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfuploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};

var albumobj = {};
function SetAlbumUpload(strid, bolmore, moreparam)
{
	if (typeof(bolmore)=='undefined' || bolmore==null)
	{
		bolmore = 0;
	}
	if (typeof(moreparam)=='undefined' || moreparam==null)
	{
		moreparam = '';
	}
	var settings_object = {
		// Backend Settings
		upload_url: WEBURLPATH+"/upload.php?"+moreparam,

		// File Upload Settings
		file_size_limit : "2 MB",	// 2MB
		file_types : "*.jpg;*.jpeg;*.gif;*.png",
		file_types_description : "图片格式",
		file_upload_limit : bolmore,

		// Event Handler Settings - these functions as defined in Handlers.js
		//  The handlers are not part of SWFUpload but are part of my website and control how
		//  my website reacts to the SWFUpload events.
		swfupload_preload_handler : preLoad,
		swfupload_load_failed_handler : loadFailed,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,

		// Button Settings
		button_image_url : "",
		button_placeholder_id : "albumup_button_"+strid,
		button_width: 150,
		button_height: 25,
		button_text : '<span class="flashuploadbutton">点击上传图片</span>',
		button_text_style : '.flashuploadbutton{font-size: 14px; font-family: "微软雅黑", Tahoma, Verdana, Arial, Helvetica, sans-serif; color: #444444;}',
		button_text_top_padding: 1,
		button_text_left_padding: 30,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,
		
		// Flash Settings
		flash_url : WEBURLPATH+"/js/swfupload/swfupload.swf",
		flash9_url : WEBURLPATH+"/js/swfupload/swfupload_FP9.swf",

		custom_settings : {
			upload_sign: strid,
			upload_filenum: bolmore,
			upload_target: "albumup_progress_"+strid,
			upload_show: "albumup_thumb_"+strid
		},
		
		// Debug Settings
		debug: false
	};
	albumobj[strid] = new SWFUpload(settings_object);
	albumobj[strid+'_idcount'] = 0;
}